package ldh.im.fxgui.component.message;

import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.data.pojo.status.ImMessageStatus;
import ldh.im.fxgui.handle.IMessageEditorHandler;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxgui.component.message.bo.WrapImMessage;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public interface MessageListViewHandler extends MessageHandler {

    Node createMessagePaneNode();

    // 判断消息是否能够显示
    boolean canShowMessage(ImMessage message);

    boolean addMessage(ImMessage message);

    default void deleteMessage(ImMessage message) {
        getItems().remove(message);
    }

    void addMessage(int index, ImMessage message);

    void updateMessage(ImMessage message);

    void updateMessages(List<ImMessage> messages);

    void addMessages(List<ImMessage> messages);

    void addFrontMessages(List<ImMessage> messages);

    void addLastedMessages(List<ImMessage> messages);

    default Pane createMessagePane(WrapImMessage textMessage) {
        try {
            MessageView messageView = new MessageView(this, textMessage);
            return messageView;
        } catch (Exception e) {
            e.printStackTrace();
            ImMessage message = new ImMessage();
            message.setMessageType(MessageType.Text);
            message.setContent("error");
            message.setStatus(ImMessageStatus.readed);
            return new MessageView(this, WrapImMessage.wrap(message));
        }
    }

    ObservableList<WrapImMessage> getItems();

    void setImSession(ImSession imSession);

    void refresh();

    ImSession getMessageSession();

    IMessageEditorHandler getMessageEditorService();

    ImMessage getFirstMessage();

    ImMessage getLastedMessage();

    Set<Long> getMessageIdSet();

    void setDialogRoot(StackPane stackPane);

    default void showUp() {}

    default void showDown() {}

    default void update(ImMessage message) {

    }

    default void remove(ImMessage message) {

    }

    default void clean() {

    }

    default List<ImMessage> getSelectedMessages() {
        return getItems().stream().filter(w->w.isSelectProperty()).map(w->w.getImMessage()).collect(Collectors.toList());
    }
}
